const app = getApp()
let baseURL = app.globalData.config.baseURL;
Component({
	options: {
		multipleSlots: true,
		addGlobalClass: true
	},
	/**
	 * 组件的属性列表
	 */
	properties: {
		initLoad: {
			type: Boolean,
			value: false
		},
		refreshState: {
			type: Number,
			value: 0
		},
		pushState: {
			type: Number,
			value: 0
		},
		//禁用下拉
		disableDown: {
			type: Boolean,
			value: false
		}
	},
	observers: {
		'refreshState': function (refreshState) {
			if (refreshState == 1) {
				this.setData({
					pull: this.data.refreshSuccess
				}, () => {
					setTimeout(() => {
						this.setData({
							pullIsLoading: false,
							isTask: false,
							...(!this.data.initSuccess ? {
								initSuccess: true
							} : {})
						})
					}, 1000)
				})
			}
		},
		'pushState': function (pushState) {
			let obj = {
				"0": {
					pushIsLoading: false,
					push: this.data.pushSuccess
				},
				"1": {
					pushIsLoading: true,
					push: this.data.pushLoading
				},
				"2": {
					pushIsLoading: false,
					push: this.data.pushSuccess,
					isTask: false,
					isFinally: false
				},
				"3": {
					pushIsLoading: false,
					push: this.data.pushFinally,
					isTask: false,
					isFinally: true
				}
			}
			this.setData(obj[pushState])
		},
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		baseURL: app.globalData.config.baseURL,
		//是否显示到顶部按钮
		isTopShow: false,
		//滚动位置
		scrollTop: 0,
		//是否在任务中
		isTask: false,
		slideStart: [],
		moveTime: 0,
		//初始化隐藏上拉容器
		initSuccess: false,
		//下拉当前配置
		pull: {},
		//上拉当前配置
		push: {},
		//下拉显示
		pullIsLoading: false,
		//上拉显示loading图片
		pushIsLoading: true,
		//下拉刷新加载配置
		refreshLoading: {
			loading: baseURL + '/chartRoom/images/common/pull_refresh.gif',
			text: '正在加载'
		},
		//下拉刷新成功配置
		refreshSuccess: {
			loading: baseURL + '/chartRoom/images/common/finish.png',
			text: '刷新完成'
		},
		//上拉刷新加载配置
		pushLoading: {
			loading: baseURL + '/chartRoom/images/common/pull_refresh.gif',
			text: '-正在加载-'
		},
		//上拉刷新成功和未操作状态配置
		pushSuccess: {
			loading: baseURL + '/chartRoom/images/common/finish.png',
			text: '-上拉加载更多-'
		},
		//上拉刷新没有数据了配置
		pushFinally: {
			loading: baseURL + '/chartRoom/images/common/pull_refresh.gif',
			text: '-没有更多数据了-'
		},
		//是否没有数据了
		isFinally: false
	},
	created() {

	},
	ready() {

	},
	attached() {
		//初始加载
		if (this.data.initLoad) {
			this.triggerEvent('refresh', {
				refresh: true
			})
		}
	},
	methods: {
		//滚动到顶部
		toTop() {
			this.setData({
				scrollTop: 0
			})
		},
		//滚动事件
		viewScroll(e) {
			let {
				scrollTop
			} = e.detail;
			this.setData({
				isTopShow: scrollTop > 1500
			})
		},
		touchstart(e) {
			this.setData({
				slideStart: e.touches[0]
			})
		},
		touchmove(e) {
			let slideStart = this.data.slideStart;
			let slideMove = e.touches[0];
			let startX = slideStart.pageX;
			let startY = slideStart.pageY;
			let moveEndX = slideMove.pageX;
			let moveEndY = slideMove.pageY;
			let X = moveEndX - startX;
			let Y = moveEndY - startY;
			if (Math.abs(Y) > Math.abs(X) && Y > 20) {
				this.pullRefresh()
			}
			// else if (Math.abs(Y) > Math.abs(X) && Y < 0) {
			// 	if (!this.data.isFinally) {
			// 		this.loadMore()
			// 	}
			// }
		},
		scrolltolower() {
			if (!this.data.isFinally) {
				this.loadMore()
			}
		},
		/**下拉刷新 */
		pullRefresh(e) {
			//任务中
			if (this.data.isTask) {
				return;
			}
			//禁用下拉
			if (this.data.disableDown) {
				return;
			}
			this.setData({
				isTask: true,
				pullIsLoading: true,
				isFinally: false,
				pull: this.data.refreshLoading
			})
			//至少执行一秒钟动画
			setTimeout(() => {
				this.triggerEvent('refresh', {
					refresh: true
				})
			}, 1000)
		},
		/**上拉加载更多 */
		loadMore(e) {
			//任务中
			if (this.data.isTask) {
				return;
			}
			this.setData({
				isTask: true,
				pushState: 1
			})
			//至少执行一秒钟动画
			setTimeout(() => {
				this.triggerEvent('toload', {
					toload: true
				})
			}, 1000)
		}
	}
})